<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="赛事名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mname">
              <a-input v-model="model.mname" placeholder="请输入赛事名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tagsnum">
              <a-input-number v-model="model.tagsnum" placeholder="请输入人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="赛事主办单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="morg">
              <a-input v-model="model.morg" placeholder="请输入赛事主办单位" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="时间间隔（秒）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeinterval">
              <a-input-number v-model="model.timeinterval" placeholder="请输入时间间隔（秒）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mtext">
              <a-textarea v-model="model.mtext" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="比赛类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="matchtype">
              <j-dict-select-tag type="list" v-model="model.matchtype" dictCode="matchtype" placeholder="请选择比赛类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="比赛状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mstatus">
              <j-dict-select-tag type="list" v-model="model.mstatus" dictCode="matchstatus" placeholder="请选择比赛状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="赛事人员" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="runnerMatchPlayerTable.loading"
          :columns="runnerMatchPlayerTable.columns"
          :dataSource="runnerMatchPlayerTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="赛事设备" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="runnerMatchDeviceTable.loading"
          :columns="runnerMatchDeviceTable.columns"
          :dataSource="runnerMatchDeviceTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="赛事组信息" :key="refKeys[2]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[2]"
          :loading="runnerMatchInfoTable.loading"
          :columns="runnerMatchInfoTable.columns"
          :dataSource="runnerMatchInfoTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'RunnerMatchForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           mname: [
              { required: true, message: '请输入赛事名称!'},
           ],
           tagsnum: [
              { required: true, message: '请输入人数!'},
           ],
           matchtype: [
              { required: true, message: '请输入比赛类型!'},
           ],
        },
        refKeys: ['runnerMatchPlayer', 'runnerMatchDevice', 'runnerMatchInfo', ],
        tableKeys:['runnerMatchPlayer', 'runnerMatchDevice', 'runnerMatchInfo', ],
        activeKey: 'runnerMatchPlayer',
        // 赛事人员
        runnerMatchPlayerTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '姓名',
              key: 'player',
              type: JVXETypes.selectSearch,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '组别',
              key: 'groupid',
              type: JVXETypes.select,
              options:[],
              dictCode:"runner_group",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '芯片号',
              key: 'ringcode',
              type: JVXETypes.selectSearch,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '参赛编号',
              key: 'pcode',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '开始时间',
              key: 'pstarttime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '总圈数',
              key: 'totalring',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:0,
            },
            {
              title: '总成绩',
              key: 'score',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '净成绩',
              key: 'scorecj',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '手机号',
              key: 'phone',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '身份证号',
              key: 'idcard',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '团队',
              key: 'teams',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '是否检录',
              key: 'iscall',
              type: JVXETypes.select,
              options:[],
              dictCode:"callstatus",
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 赛事设备
        runnerMatchDeviceTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '设备',
              key: 'device',
              type: JVXETypes.selectSearch,
              dictCode:"runner_device,dname,did",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '计时点',
              key: 'timePoint',
              type: JVXETypes.select,
              options:[],
              dictCode:"timepoit",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '设备标志',
              key: 'deviceflag',
              type: JVXETypes.select,
              options:[],
              dictCode:"deviceFlag",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 赛事组信息
        runnerMatchInfoTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '组别',
              key: 'mgroup',
              type: JVXETypes.select,
              options:[],
              dictCode:"runner_group",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '开始时间',
              key: 'mstarttime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '结束时间',
              key: 'mendtime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/match/runnerMatch/add",
          edit: "/match/runnerMatch/edit",
          queryById: "/match/runnerMatch/queryById",
          runnerMatchPlayer: {
            list: '/match/runnerMatch/queryRunnerMatchPlayerByMainId'
          },
          runnerMatchDevice: {
            list: '/match/runnerMatch/queryRunnerMatchDeviceByMainId'
          },
          runnerMatchInfo: {
            list: '/match/runnerMatch/queryRunnerMatchInfoByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.runnerMatchPlayerTable.dataSource=[]
        this.runnerMatchDeviceTable.dataSource=[]
        this.runnerMatchInfoTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.runnerMatchPlayer.list, params, this.runnerMatchPlayerTable)
          this.requestSubTableData(this.url.runnerMatchDevice.list, params, this.runnerMatchDeviceTable)
          this.requestSubTableData(this.url.runnerMatchInfo.list, params, this.runnerMatchInfoTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          runnerMatchPlayerList: allValues.tablesValue[0].tableData,
          runnerMatchDeviceList: allValues.tablesValue[1].tableData,
          runnerMatchInfoList: allValues.tablesValue[2].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>